package com.shop.cereshop.app.controller.tencent.im;

import com.shop.cereshop.app.tencent.im.model.coupon.ReceiveCouponParam;
import com.shop.cereshop.app.tencent.im.service.CouponMsgService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @description: 优惠券消息接口
 * @author zdd
 * @date 2025/11/10
 * @version 1.0
 */
@Slf4j
@Api(tags = "IM-优惠券消息")
@RestController
@RequestMapping("/im/coupon")
public class CouponMsgController {

  @Resource
  private CouponMsgService couponMsgService;

  /**
   * 用户从IM消息中领取优惠券
   * 用户点击IM优惠券消息后，调用此接口完成优惠券的领取
   *
   * @param param   优惠券领取参数
   * @param request HTTP请求
   * @return 领取结果
   */
  @PostMapping("/receive")
  @ApiOperation(value = "用户领取IM优惠券", notes = "用户点击IM优惠券消息后调用此接口")
  public Result<Void> receiveCoupon(
      @Valid @RequestBody ReceiveCouponParam param,
      HttpServletRequest request) {

    // 1. 获取当前用户信息
    CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }

    // 2. 调用服务领取优惠券
    return couponMsgService.receiveCoupon(user.getBuyerUserId(), param);
  }

}
